package com.hgsf.controller;

import com.hgsf.entity.*;
import com.hgsf.service.AdminService;
import com.hgsf.util.Page;
import com.hgsf.util.Verify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;

	@Autowired
	private HttpServletRequest request;
	@RequestMapping("/login")
	@ResponseBody
	public String login(Admin admin, String checkcode) throws IOException {
		HttpSession session = request.getSession();
		String s = "";
		if (session.getAttribute("checkcode").equals(checkcode)) {

			Admin adm = null;
			try {
				adm = adminService.login(admin);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (adm != null) {
				s="success";
				session.setAttribute("status","admin");
				session.setAttribute("name",adm.getAname());
			} else {
				session.setAttribute("status","psw_wrong");
				s="failed";
			}
		}
		else {
			session.setAttribute("status","verify_wrong");
			s="failed";
		}
		return s;
	}

	@RequestMapping("/verify")
	public void verify(HttpServletResponse response) throws IOException {
		System.out.println(111);
		String checkcode = Verify.getCheckCode();
		BufferedImage image = Verify.createImage(checkcode);
		request.getSession().setAttribute("checkcode",checkcode);
		OutputStream out = response.getOutputStream();
		Verify.writeImage(out,image);
		out.flush();
		out.close();
	}
	@RequestMapping("/show_course")
	public ModelAndView listCourse() {
		ModelAndView modelAndView = new ModelAndView();
		List<Course> datas = adminService.queryCourse();
		List<Teacher> teachers = adminService.queryTeacher();
		List<Major> majors = adminService.queryMajor();
		modelAndView.addObject("datas", datas);
		modelAndView.addObject("teachers", teachers);
		modelAndView.addObject("majors", majors);
		modelAndView.setViewName("view/list_course");
		return modelAndView;
	}
	@ResponseBody
	@RequestMapping("/list_course")
	public Map<String,Object> listCourseByCondition(String condition){
		System.out.println(condition);
		List<Course> datas = adminService.queryCourseByCondition(condition);
		List<Teacher> teachers = adminService.queryTeacher();
		List<Major> majors = adminService.queryMajor();

		Map<String,Object> map = new HashMap<>();
		map.put("datas",datas);
		map.put("teachers",teachers);
		map.put("majors",majors);
		return map;
	}

	@ResponseBody
	@RequestMapping("/delete_course")
	public String deleteCourse(@RequestParam int [] cid) {
		System.out.println(cid);
		boolean b[] = new boolean[cid.length];
		for (int i = 0; i < cid.length; i++) {
			b[i]= adminService.delectCourse(cid[i]);
		}
		for (int i = 0; i < b.length; i++) {
			if(!b[i]) return "failed";
		}

		return "success";
	}
	@RequestMapping("/add_student_form")
	public ModelAndView addStuForm(){
		ModelAndView modelAndView = new ModelAndView("view/add_student");
		List<Major> majors = adminService.queryMajor();
		modelAndView.addObject("majors", majors);
		return modelAndView;

	}
	@ResponseBody
	@RequestMapping("/add_student")
	public String addStudent(Student s){
		System.out.println(s.toString());
		boolean b = false;
		try {
			b = adminService.addStudent(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(b){
			return "success";
		}

		return "failed";

	}

	@ResponseBody
	@RequestMapping(value="/alter_course")
	public String alterCourse(Course course) {
		System.out.println(course.toString());
		boolean b = false;
		try {
			b = adminService.alterCourse(course);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(b) {
			return "success";
		}
		return "failed";
	}

	@RequestMapping("/loginin")
	public ModelAndView loginin() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("loginin");
		return mav;
	}
	@RequestMapping("/add_course_form")
	public ModelAndView addCourseForm() {
		ModelAndView modelAndView = new ModelAndView();
		List<Teacher> teachers = adminService.queryTeacher();
		List<Major> majors = adminService.queryMajor();
		modelAndView.addObject("teachers", teachers);
		modelAndView.addObject("majors", majors);
		modelAndView.setViewName("view/add_course");
		return modelAndView;
	}
	@ResponseBody
	@RequestMapping("/add_course")
	public String addCourse(Course course) {
		System.out.println(course.toString());

		boolean b = false;
		try {
			b = adminService.addCourse(course);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(b) {
			return "success";
		}
		return "failed";
	}


	@RequestMapping("/list_student")
	public ModelAndView queryStudentByCondition(String condition){
		List<Student> students = adminService.queryStudentByCodition("");
		ModelAndView mav = new ModelAndView();
		List<Major> majors = adminService.queryMajor();
		System.out.println(students);
		mav.addObject("students",students);
		mav.addObject("majors",majors);
		mav.setViewName("view/list_student");
		return mav;
	}

	@ResponseBody
	@RequestMapping(value = "/alter_student")
	public String alterStudent(Student student){
		System.out.println(student.toString());

		boolean b = false;
		try {
			b = adminService.alterStudent(student);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(b) {
			return "success";
		}
		return "failed";
	}
	@RequestMapping("list_teacher")
	public ModelAndView listTeacher(){
		ModelAndView modelAndView = new ModelAndView("view/list_teacher");
		List<Map<String,String>> teachers = adminService.queryTeacherByCondition("");
		System.out.println(teachers);
		modelAndView.addObject("teachers",teachers);
		return  modelAndView;
	}
	@ResponseBody
	@RequestMapping("list_teacher_by_condition")
	public List<Map<String,String>> listTeacherByCondition(String condition){
		return adminService.queryTeacherByCondition(condition);
	}
	@ResponseBody
	@RequestMapping("alter_teacher")
	public String alterTeacher(@RequestParam  Map<String,String> map){
		System.out.println(map.toString());
		boolean b = false;
		try {
			b = adminService.alterTeacher(map);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(b) {
			return "success";
		}
		return "failed";
	}

	@ResponseBody
	@RequestMapping("delete_teacher")
	public String deleteTeacher(@RequestParam("tid") List<Integer> tid){
		for(int t:tid){
			if(!adminService.deleteTeacher(t)){
				return "failed";
			}
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping("reset_admin_psw")
	public String resetAdminPsw(int aid){
		boolean b = false;
		try {
			b = adminService.resetAdminPsw(aid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(b){
			return "success";
		}
		return "failed";
	}
	@ResponseBody
	@RequestMapping("reset_teacher_psw")
	public String resetTeacherPsw(int tid){
		boolean b = false;
		try {
			b = adminService.resetTeacherPsw(tid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(b){
			return "success";
		}
		return "failed";
	}
	@ResponseBody
	@RequestMapping("reset_student_psw")
	public String resetStudentPsw(long sid){
		boolean b = false;
		try {
			b = adminService.resetStudentPsw(sid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(b){
			return "success";
		}
		return "failed";
	}
	@ResponseBody
	@RequestMapping("/delete_student")
	public String deleteStudent(@RequestParam("sid") List<Long>  sid){
		for(long s:sid){
			if(!adminService.deleteStudent(s)){
				return "failed";
			}
		}
		return "success";
	}
	@ResponseBody
	@RequestMapping("/add_major")
	public String addMajor(Major m){
		boolean b = false;
		try {
			b = adminService.addMajor(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(b){
			return "success";
		}
		return "failed";
	}
	@ResponseBody
	@RequestMapping("/add_major_form")
	public ModelAndView addMajorForm(){
		ModelAndView modelAndView = new ModelAndView("view/add_major");
		List<College> colleges = adminService.queryCollege();

		modelAndView.addObject("colleges",colleges);
		return modelAndView;
	}
	@RequestMapping("my_blank")
	public ModelAndView my_blank(){
		return new ModelAndView("view/my_blank");
	}
	@RequestMapping("add_teacher_form")
	public ModelAndView addTeacherForm(){
		return new ModelAndView("view/add_teacher");
	}
	@ResponseBody
	@RequestMapping("add_teacher")
	public String addTeacher(Teacher t){

		boolean b = false;
		try {
			b = adminService.addTeacherMsg(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(b){
			return "success";
		}
		return "failed";
	}
	@RequestMapping("/query_course_by_page")
	public ModelAndView queryCourseByPage(int currentPage){
		ModelAndView modelAndView = new ModelAndView("view/my_blank");
		HttpSession session = request.getSession();
		Object cp = session.getAttribute("cp");
		List<Teacher> teachers = adminService.queryTeacher();
		List<Major> majors = adminService.queryMajor();
		modelAndView.addObject("teachers", teachers);
		modelAndView.addObject("majors", majors);
		if(cp==null){
			Page<Map<String,String>> page = new Page<>();
			List<Map<String,String>> datas = adminService.queryCourseByPage(page);
			page.setDatas(datas);
			page.setTotalCount(adminService.queryTotalCourseCount());
			session.setAttribute("cp",page);
		}
		else {
			Page<Map<String,String>> page = (Page<Map<String,String>>)session.getAttribute("cp");
			page.setCurrentPage(currentPage);
			List<Map<String,String>> datas = adminService.queryCourseByPage(page);
			page.setDatas(datas);
			System.out.println(page.getTotalPage());
			session.setAttribute("cp",page);
		}
		return modelAndView;
	}
	/**
	 * 列出已选课程
	 * @return
	 */
	@RequestMapping("selected_course")
	public ModelAndView selectedCourse(){
		ModelAndView modelAndView = new ModelAndView("view/selected_course");
		List<Map<String,String>> datas = adminService.queryStuSelectedCourse("");
		List<Course> courses = adminService.queryCourse();
		modelAndView.addObject("datas",datas);
		modelAndView.addObject("courses",courses);
		return modelAndView;
	}
	@RequestMapping("deleteSelectedCourse")
	@ResponseBody
	public String deleteSelectedCourse(@RequestBody List<SelectedCourse> selectedCourses){
		boolean b = false;
		try {
			b = adminService.deleteSelectedCourse(selectedCourses);
			} catch (Exception e) {
			e.printStackTrace();
		}
		if(b){
			return "success";
		}
		return "failed";
	}
}
